package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-30
 * Time: 9:35
 */
public class TestDemo {

    /**
     * 输入链表头节点，奇数长度返回中点，偶数长度返回上中点
     * 输入链表头节点，奇数长度返回中点，偶数长度返回下中点
     * 输入链表头节点，奇数长度返回中点前一个，偶数长度返回上中点前一个
     * 输入链表头节点，奇数长度返回中点前一个，偶数长度返回下中点前一个
     */

    public static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }

    //输入链表头节点，奇数长度返回中点，偶数长度返回上中点
    public static ListNode minNode1(ListNode head){
        if(head==null||head.next==null||head.next.next==null){
            return head;
        }
          ListNode fast = head.next;
          ListNode slow = head;
          while(fast!=null&&fast.next!=null){
              fast = fast.next.next;
              slow = slow.next;
          }
          return slow;
    }
    //输入链表头节点，奇数长度返回中点，偶数长度返回下中点
    public static ListNode minNode2(ListNode head){
        if(head==null||head.next==null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast!=null&&fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //输入链表头节点，奇数长度返回中点前一个，偶数长度返回上中点前一个
    public static ListNode midOrUpMidPreNode(ListNode head) {
         if(head == null || head.next==null||head.next.next == null){
             return null;
         }
         ListNode fast = head.next.next;
         ListNode slow = head;
         while(fast.next!=null&&fast.next.next!=null){
             fast = fast.next.next;
             slow = slow.next;
         }
         return slow;
    }

    //输入链表头节点，奇数长度返回中点前一个，偶数长度返回下中点前一个(上中点)
    public static ListNode midOrDownMidPreNode(ListNode head) {
         if(head==null || head.next==null){
             return null;
         }
         if(head.next.next==null){
             return head;
         }
         ListNode slow = head;
         ListNode fast = head.next;
         while(fast.next!=null&&fast.next.next!=null){
             fast = fast.next.next;
             slow = slow.next;
         }
         return slow;
    }

    public static void main(String[] args) {


    }


}
